Install Deps
In [ ]:
import numpy as np
import tensorflow as tf
import tflearn
import spacy
nlp = spacy.load('en')
import re
from nltk.util import ngrams, trigrams
import csv
from tflearn.data_utils import to_categorical
Load the model
In [ ]:
import csv
word2idx = {}
csv_len = 0
for key, val in csv.reader(open("../models/participlevocabindex.csv")):
word2idx[key] = int(val)
csv_len += 1
word2idx
In [ ]:
# Network building
def build_model():
# This resets all parameters and variables, leave this here
tf.reset_default_graph()
#### Your code ####
net = tflearn.input_data([None, csv_len]) # Input
net = tflearn.fully_connected(net, 200, activation='ReLU') # Hidden
net = tflearn.fully_connected(net, 25, activation='ReLU') # Hidden
net = tflearn.fully_connected(net, 2, activation='softmax') # Output
net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')
model = tflearn.DNN(net)
return model
model = build_model()
In [ ]:
model.load('../models/participle_model.tfl')
Import the vocab index
Import the dataprep methods
In [ ]:
def textStringToPOSArray(text):
doc = nlp(text)
tags = []
for word in doc:
tags.append(word.tag_)
return tags
def find_ngrams(input_list, n):
return zip(*[input_list[i:] for i in range(n)])
def getPOSTrigramsForTextString(text):
tags = textStringToPOSArray(text)
tgrams = list(trigrams(tags))
return tgrams
def trigramsToDictKeys(trigrams):
keys = []
for trigram in trigrams:
keys.append('>'.join(trigram))
return keys
def textToTrigrams(text):
return trigramsToDictKeys(getPOSTrigramsForTextString(text))
def text_to_vector(text):
wordVector = np.zeros(csv_len)
for word in textToTrigrams(text):
index = word2idx.get(word, None)
if index != None:
wordVector[index] += 1
return wordVector
def test_sentence(sentence):
positive_prob = model.predict([text_to_vector(sentence)])[0][1]
print('Sentence: {}'.format(sentence))
print('P(positive) = {:.3f} :'.format(positive_prob),
'Positive' if positive_prob > 0.5 else 'Negative')
return positive_prob
Try it out!
In [ ]:
test_sentence("Using up all his energy.")
In [ ]:
test_sentence("Tom, using up all his energy, ate the taco.")
In [ ]:
test_sentence("Standing on the edge of the cliff looking down, he could see a boat.")
In [ ]:
test_sentence("Standing on the edge of the cliff looking down.")
In [ ]:
test_sentence("There was Emilia, dripping in sadness")
In [ ]:
test_sentence("Dripping in sadness")
In [ ]: